#include "stdafx.h"
#include "EntityBuilderWorld.h"
#include "Entity/EntityManagerIf.h"
#include "Entity/EntityR.h"
#include "Combat/CombatSysP.h"



EntityBuilderAddRoles::EntityBuilderAddRoles(IEntity* pEntity, MsgLoadRoles& msg)
:the_msg(msg), entity(pEntity)
{
}

EntityBuilderAddRoles::~EntityBuilderAddRoles()
{
}

IEntity*
EntityBuilderAddRoles::build()
{
	IEntityManager* pEttSubContainer = NULL;
	if (entity)
	{
		pEttSubContainer = entity->getEntityMgr();
	}

	if (pEttSubContainer==NULL)
	{
		return NULL;
	}



	for (MsgLoadRoles::DBRoleList::iterator it = the_msg.roles.begin();
		it!=the_msg.roles.end();
		it++)
	{
		DBRole& dbrole = *it;
		IEntity* pEttSub = new EntityR;
		pEttSub->setGlobalID(dbrole.rid);
		pEttSub->setLevel(dbrole.lvl);
		pEttSub->setStaticID(dbrole.hid);
		pEttSub->setProps(Entity::ATTR_EXP, dbrole.exp);
		pEttSub->setProps(Entity::ATTR_ELVL, dbrole.elvl);
		for(int i = Entity::ESLOT_HEAD; i < Entity::ESLOT_END; ++i)
		{
			pEttSub->setProps(i, dbrole.eslot[i-1]);
		}

		//
		// init combat system for current role
		pEttSub->setCombatSys( new CombatSysP );

		ICombatSys* cs = pEttSub->getCombatSys();
		if(cs)
		{
			cs->setHP(dbrole.hp);
			cs->setMP(dbrole.mp);
			//
			//	role max hp/mp will be initialized in lua script when loaded
			//
			//cs->setMaxHP(500);
			//cs->setMaxMP(500);
		}

		pEttSubContainer->addEntity(dbrole.rid, pEttSub);
	}
	return NULL;
}
